#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <sys/epoll.h>

#include "socket.h"
#include "epoll.h"
#include "data_packet.h"
#include "like.h"

void perr_exit(const char *err)
{
	perror(err);
	exit(1);
}

struct sockaddr_in sockaddr_init(struct sockaddr_in addr)
{
	bzero(&addr, sizeof(addr));    
	addr.sin_family = AF_INET;    
	addr.sin_addr.s_addr = htonl(INADDR_ANY);    
	addr.sin_port = htons(SERV_PORT);
	 
	return addr;
}

int socket_init(struct sockaddr_in addr)
{
	int ret,sock_fd;
	sock_fd = socket(AF_INET,SOCK_STREAM , 0);
	if(sock_fd==-1)
		return -1;
	int opt = 1;    
	setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 
	
	ret = bind(sock_fd,(struct sockaddr *)&addr,sizeof(addr));
	if(ret==-1)
		perr_exit("bind error");
	ret = listen(sock_fd,LINK_MAX);
	if(ret == -1)
		perr_exit("listen error");  

	return sock_fd;
}

